 (function(){
	       var obj={};
	       //用来设置ajax回调函数的上下文。让回调函数中的this指向这个对象。
	       obj.ajax = function(type, url, data, headers, context, callback){
               $.ajax({
                    type: type,
			         url: url,
			         data: JSON.stringify(data),
			         headers: headers,
			         context:context
		          }).done(callback);
	           };
	       window.ajaxRequest = obj;
        })();
        
        function orderListShow(template){
            this.orderTemplate = template;
            this.orderModel = null;
            this.findOrderByStatus = function(status){
                /*添加查询操作,根据spring提供的借口做出修改*/
                ajaxRequest.ajax("GET","/spring-merchant/orders/status?order_status_id="+status,null,null,this,function(orders){
			        this.orderModel = orders;
			        _render_order(this.orderTemplate, this.orderModel);
		     });
            }
            
            function dateFormat(time){
                var birth = new Date(time);
                var month = birth.getMonth()+1;
                if (birth.getMonth()+1<10){
                    var month = "0" + (birth.getMonth()+1);
                }
                var dat = birth.getDate();
                if (birth.getDate() < 10){
                    var dat = "0" + birth.getDate();
                }
                return birth.getFullYear()+"-"+month+"-"+dat;
            }
            
            function bindmenue_operation(li){
            	li.on("click",function(){
            		var tt = $(this);
            		ajaxRequest.ajax("PUT","/spring-merchant/orders/"+tt.attr("id")+"/"+tt.text(),null,null,this,function(){
            					$(this).parent().parent().siblings("[name=status]").children("p").text(tt.text());
            					swal("success");
            		});
            	});
            }
            
            /*渲染orderList*/
            function _render_order(orderTemplate,data){
            	var orderTemplateTemp = orderTemplate;
            	orderTemplateTemp.empty();
                /*添加渲染动作，等模板写好后添加*/
                data.forEach(function(item){
                    var clone = $("#order-sample").clone().removeClass("hidden-attr");
                    var childOdClone = clone.children("#order-details-list");
                    /*date*/
                    childOdClone.children("div:first").children("h3").text();
                    /*time*/
                    var  newDate = new Date();
                    newDate.setTime(item.createDate*1000);
                    childOdClone.children("div:first").children("p").text(newDate.toDateString());
 
                    /*customer name*/
                    childOdClone.children("div:nth-child(2)").children("h3").text(item.customer.name);
                    /*customer address*/
                    childOdClone.children("div:nth-child(2)").children("p").text(item.deliveryAddress);
                    /*Money Total*/
                    var total = 0;
                    item.orderItemList.forEach(function(item){
                    	total = total + item.price*item.amount;
                    });
                    childOdClone.children("div:nth-child(3)").children("h3").text("Total");
                    /*Proess Number*/
                    childOdClone.children("div:nth-child(3)").children("p").text(total);
                    /*Order Statu*/
                    childOdClone.children("div:nth-child(4)").children("h3").text("status");
                    childOdClone.children("div:nth-child(4)").children("p").text(item.status).attr("id","status"+item.id);
                    
                    childOdClone.children("div:nth-child(5)").children("h3").text("processNumber");
                    childOdClone.children("div:nth-child(5)").children("p").text(item.processNumber);
                    /* detail button*/
                    childOdClone.children("div:nth-child(6)").children("button:first").attr("id","detailBtn").attr("value", item.processNumber);
                    childOdClone.children("div:nth-child(6)")
                        .children("button#detailBtn")
                        .on("click", function(){
                             window.location.href="/spring-merchant/orders/detail?order_processNumber_id="+$(this).attr("value");
                        });
                   childOdClone.children("div:nth-child(6)").children("ul").children("li").attr("id",item.id);
                 
                   var operator_list = childOdClone.children("div:nth-child(6)").children("ul");
                   console.log(item);
          		 	console.log(operator_list.attr("class"));
        			if("WAIT" == item.status){
        				 operator_list.children("li:nth-child(3)").addClass("hidden-attr");
        			}else if("REJECT" == item.status || "DELIVER" == item.status||"SUCCESS" == item.status){
        				 operator_list.children("li").addClass("hidden-attr");
			         }else if("RECEIVE" == item.status){
			        	 operator_list.children("li:nth-child(1)").addClass("hidden-attr");
			        	 operator_list.children("li:nth-child(2)").addClass("hidden-attr");
			         }else{
			        	 operator_list.children("li").addClass("hidden-attr");
			         }
        			operator_list.children("li:nth-child(1)").on("click", function(){
        				operator_list.children("li:nth-child(1)").addClass("hidden-attr");
        				operator_list.children("li:nth-child(2)").addClass("hidden-attr");
        				operator_list.children("li:nth-child(3)").removeClass("hidden-attr");
        			});
        			operator_list.children("li:nth-child(2)").on("click", function(){
        				operator_list.children("li:nth-child(1)").addClass("hidden-attr");
        				operator_list.children("li:nth-child(2)").addClass("hidden-attr");
        			});
        			operator_list.children("li:nth-child(3)").on("click", function(){
        				operator_list.children("li:nth-child(3)").addClass("hidden-attr");
        			});
                    /* add to dom tree */
                    orderTemplateTemp.append(clone);
                    
                });
                bindmenue_operation($("li[class^=li_status]"));
            }
        };
        $(function(){
            var orderShow = new orderListShow($("#show-order-area"));
            orderShow.findOrderByStatus("ALL"); 
            /*var clone = $("#order-sample").clone();
            clone.children("#order-details-list").children("div:nth-child(4)").children("h3").text("dawduaoiwl");
            clone.children("#order-details-list").children("div:nth-child(5)").children("button#detailBtn").attr("value", "sssss");
            clone.appendTo($("#show-order-area"))*/
            
            $("li[name=orderChange]").on("click", function(){
                $(this).siblings().filter(".active").removeClass("active");
                $(this).addClass("active");
                orderShow.findOrderByStatus($(this).attr("value"));
            });
        });